package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatBanner;
import com.happy.cat.project.system.service.ICatBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * bannerController
 *
 * @author verne
 * @date 2020-07-05
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/banner" )
@Api( value="轮播图API",tags="轮播图API")
public class CatBannerController extends BaseController {

    private final ICatBannerService iCatBannerService;

    /**
     * 查询banner列表
     */
    @ApiOperation(value = "轮播图列表", notes = "轮播图列表", response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatBanner catBanner)
    {
        startPage();
        LambdaQueryWrapper<CatBanner> lqw = new LambdaQueryWrapper<CatBanner>();
        if (StringUtils.isNotBlank(catBanner.getPostion())){
            lqw.eq(CatBanner::getPostion ,catBanner.getPostion());
        }
//        lqw.eq(CatBanner::getStatus ,1);
        List<CatBanner> list = iCatBannerService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出banner列表
     */
    @ApiOperation(value = "导出banner列表", notes = "导出banner列表", response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:banner:export')" )
    @Log(title = "banner" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatBanner catBanner) {
        LambdaQueryWrapper<CatBanner> lqw = new LambdaQueryWrapper<CatBanner>(catBanner);
        List<CatBanner> list = iCatBannerService.list(lqw);
        ExcelUtil<CatBanner> util = new ExcelUtil<CatBanner>(CatBanner. class);
        return util.exportExcel(list, "banner" );
    }

    /**
     * 获取banner详细信息
     */
    @ApiOperation(value = "获取banner详细信息", notes = "获取banner详细信息", response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:banner:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatBannerService.getById(id));
    }

    /**
     * 新增banner
     */
    @ApiOperation(value = "新增banner", notes = "新增banner", response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:banner:add')" )
    @Log(title = "banner" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatBanner catBanner) {
        catBanner.setCreateTime(new Date());
        catBanner.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatBannerService.save(catBanner) ? 1 : 0);
    }

    /**
     * 修改banner
     */
    @ApiOperation(value = "修改banner", notes = "修改banner", response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:banner:edit')" )
    @Log(title = "banner" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatBanner catBanner) {
        return toAjax(iCatBannerService.updateById(catBanner) ? 1 : 0);
    }

    /**
     * 删除banner
     */
    @ApiOperation(value = "删除banner", notes = "删除banner", response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:banner:remove')" )
    @Log(title = "banner" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatBannerService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
